package ei

import (
	"math"
)

func addFinBoxEx(ip *Interpreter) {
	ip.AddFunc("PF", func(t Token) Token {
		var i,n float64
		ParseArgs(t, &i, &n)
		return FromFloat64(PF(i, n))
	})
	ip.AddFunc("FP", func(t Token) Token {
		var i,n float64
		ParseArgs(t, &i, &n)
		return FromFloat64(FP(i, n))
	})
	ip.AddFunc("PA", func(t Token) Token {
		var i,n float64
		ParseArgs(t, &i, &n)
		return FromFloat64(PA(i, n))
	})
	ip.AddFunc("FA", func(t Token) Token {
		var i,n float64
		ParseArgs(t, &i, &n)
		return FromFloat64(FA(i, n))
	})
	ip.AddFunc("AP", func(t Token) Token {
		var i,n float64
		ParseArgs(t, &i, &n)
		return FromFloat64(AP(i, n))
	})
	ip.AddFunc("AF", func(t Token) Token {
		var i,n float64
		ParseArgs(t, &i, &n)
		return FromFloat64(AF(i,n))
	})
	ip.AddFunc("i2r", func(t Token) Token {
		var i, f float64
		ParseArgs(t, &i, &f)
		return FromFloat64(i2r(i, f))
	})
	ip.AddFunc("r2i", func(t Token) Token {
		var r, f float64
		ParseArgs(t, &r, &f)
		return FromFloat64(r2i(r, f))
	})
	ip.AddFunc("CompoundTimes", func(t Token) Token {
		var i, times float64
		ParseArgs(t, &i, &times)
		return FromFloat64(CompoundTimes(i, times))
	})
}

func PF(i float64, n float64) float64 {
	return 1 / math.Pow(1+i, n)
}

func FP(i float64, n float64) float64 {
	return math.Pow(1+i, n)
}

func PA(i float64, n float64) float64 {
	return (1 - math.Pow(1+i, -n)) / i
}

func FA(i float64, n float64) float64 {
	return (math.Pow(1+i, n) - 1) / i
}

func AP(i float64, n float64) float64 {
	return i / (1 - math.Pow(1+i, -n))
}

func AF(i float64, n float64) float64 {
	return i / (math.Pow(1+i, n) - 1)
}

func i2r(i float64, inflation float64) float64 {
	return (1 + i) / (1 + inflation) - 1
}

func r2i(r float64, inflation float64) float64 {
	return (1 + r) * (1 + inflation) - 1
}

// 一年复利多次
// i: 名义年利率， times: 一年内复利次数
func CompoundTimes(i float64, times float64) float64 {
	return math.Pow((1+i/times), times) - 1
}